import axios, { AxiosRequestConfig } from 'axios';

const service = axios.create({
	timeout: 30000,
});

service.interceptors.request.use(
	(config) => {
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

service.interceptors.response.use(
	(response) => {
		return response;
	},
	(error) => {
		return Promise.reject(error);
	}
);

interface IBrsFetch<T> {
	code: number;
	message: string;
	data: T;
}

export const brsFetch = <T>(config: AxiosRequestConfig): Promise<IBrsFetch<T>> => {
	return new Promise((resolve, reject) => {
		service
			.request(config)
			.then((res) => {
				resolve(res.data);
			})
			.catch((err) => {
				reject(err);
			});
	});
};

export default service;
